home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / libg_261.zip / libg_261 / libio / dbz / fake.c < prev    next >
C/C++ Source or Header  |  1993-10-25  |  3KB  |  144 lines

  1. /*
  2.  * fake - make up random lines resembling history-file entries, reproducibly
  3.  *
  4.  * -Log-
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <string.h>
  11.  
  12. #define    MAXSTR    500        /* For sizing strings -- DON'T use BUFSIZ! */
  13. #define    STREQ(a, b)    (*(a) == *(b) && strcmp((a), (b)) == 0)
  14.  
  15. #ifndef lint
  16. static char RCSid[] = "$Header: /rel/cvsfiles/devo/libio/dbz/fake.c,v 1.2 1993/10/25 20:02:42 bothner Exp $";
  17. #endif
  18.  
  19. int midonly = 0;        /* just message ids, rest not realistic */
  20. int tag = 0;            /* tag lines with random digit for later use */
  21. int expired = -1;        /* percentage of lines to be expired */
  22.  
  23. int debug = 0;
  24. char *progname;
  25.  
  26. char *inname;                /* filename for messages etc. */
  27. long lineno;                /* line number for messages etc. */
  28.  
  29. void doline();
  30. void addchars();
  31. void seed();
  32.  
  33. /*
  34.  - main - parse arguments and handle options
  35.  */
  36. main(argc, argv)
  37. int argc;
  38. char *argv[];
  39. {
  40.     int c;
  41.     int errflg = 0;
  42.     FILE *in;
  43.     struct stat statbuf;
  44.     extern int optind;
  45.     extern char *optarg;
  46.     void process();
  47.     register long no;
  48.     extern long atol();
  49.     char line[MAXSTR];
  50.  
  51.     progname = argv[0];
  52.  
  53.     while ((c = getopt(argc, argv, "ms:te:d")) != EOF)
  54.         switch (c) {
  55.         case 'm':    /* message-ids only */
  56.             midonly = 1;
  57.             break;
  58.         case 's':    /* seed */
  59.             seed(atol(optarg));
  60.             break;
  61.         case 't':    /* tag lines with a random digit */
  62.             tag = 1;
  63.             break;
  64.         case 'e':    /* percentage to be expired */
  65.             expired = atoi(optarg);
  66.             break;
  67.         case 'd':    /* Debugging. */
  68.             debug++;
  69.             break;
  70.         case '?':
  71.         default:
  72.             errflg++;
  73.             break;
  74.         }
  75.     if (errflg || optind != argc - 1) {
  76.         fprintf(stderr, "usage: %s ", progname);
  77.         fprintf(stderr, "[-m] [-s seed] length\n");
  78.         exit(2);
  79.     }
  80.  
  81.     for (no = atol(argv[optind]); no > 0; no--) {
  82.         doline(line);
  83.         puts(line);
  84.     }
  85. #ifdef DBZ_FINISH
  86.     DBZ_FINISH;
  87. #endif
  88.     exit(0);
  89. }
  90.  
  91. /*
  92.  - doline - generate random history pseudo-line
  93.  */
  94. void
  95. doline(buf)
  96. char *buf;
  97. {
  98.     char tagch[2];
  99.  
  100.     (void) strcpy(buf, "<");
  101.     addchars(buf, range(4, 20));
  102.     (void) strcat(buf, "@");
  103.     addchars(buf, range(8, 20));
  104.     if (midonly)
  105.         (void) strcat(buf, ">\tx");
  106.     else {
  107.         if (tag) {
  108.             tagch[0] = "1234567890"[range(0,9)];
  109.             tagch[1] = '\0';
  110.             (void) strcat(buf, ">\t");
  111.             (void) strcat(buf, tagch);
  112.             (void) strcat(buf, "00000000~-");
  113.         } else
  114.             (void) strcat(buf, ">\t1234567890~-");
  115.     }
  116.     if (range(1, 100) > expired) {
  117.         if (midonly)
  118.             (void) strcat(buf, "\tx");
  119.         else {
  120.             (void) strcat(buf, "\t");
  121.             addchars(buf, range(10, 30));
  122.         }
  123.     }
  124. }
  125.  
  126. /*
  127.  - addchars - generate n random characters suitable for history file
  128.  */
  129. void
  130. addchars(buf, len)
  131. char *buf;
  132. int len;
  133. {
  134.     register int i;
  135.     register char *p = buf + strlen(buf);
  136.     static char vocab[] = "1234567890.abcde.fghij.klmno.pqrst.uvwxyz.\
  137. 1234567890.ABCDE.FGHIJ.KLMNO.PQRST.UVWXYZ.1234567890.\
  138. 1234567890.abcde.fghij.klmno.pqrst.uvwxyz.1234567890";
  139.  
  140.     for (i = len; i > 0; i--)
  141.         *p++ = vocab[range(0, sizeof(vocab)-2)];
  142.     *p++ = '\0';
  143. }
  144.